<?php
/**
 * @description : the failure callback page
 * @author      : stephen.mo
 * @date        : 2010/09/10
 */
require_once "TVB/OAuth/common.inc.php";
#ini_set('display_errors','on');

$call_back = (isset($_REQUEST['callback'])) ? $_REQUEST['callback'] : '#';
$pukey     = (isset($_REQUEST['pukey'])) ? $_REQUEST['pukey'] : NULL;
$lang      = (isset($_REQUEST['lang'])) ? $_REQUEST['lang'] : 'zh-hant-hk';
$order_ref = (isset($_REQUEST['Ref'])) ? $_REQUEST['Ref'] : NULL;

$base_server_path = OAuthConfig::OAuthDomainUrl($_SERVER);
$plan_message_url = $base_server_path.OAuthConfig::DEFAULT_SHOW_MESSAGE_URL;
$plan_payment_url = $base_server_path.OAuthConfig::DEFAULT_PAYMENT_URL;

$tmp_error_url = NULL;
if (empty($call_back) || empty($pukey) || empty($order_ref)) {
    $tmp_error_url = OAuthUtils::genericError(array(1030));
    OAuthUtils::redirectTo($tmp_error_url);
    exit;
}

//get some pkg info.
$ptoken   = OAuthUtils::getPToken();
$formData = OAuthUtils::getFormData($pukey,$ptoken);
if ($formData->retFlag != 1) {
    $tmp_error_url = OAuthUtils::genericError(array(1030));
}
if (!empty($tmp_error_url)) {
    OAuthUtils::redirectTo($tmp_error_url);
    exit;
}

$reback_url = '#';
if ($formData->retFlag==1) {
    $tmpData = $tmpData->data;
    $reback_url = (isset($tmpData->m_reback_url)) ? $tmpData->m_reback_url : NULL;
}

//failure callback 
$formVals['pukey']     = $pukey;
$formVals['ptoken']    = $ptoken;
$formVals['order_ref'] = $order_ref;
$formVals['referer']   = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : '';
$formVals['user_ip']   = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
$formVals['lang']      = $lang;
$ret = OAuthModel::getInstance()->failCallBack($formVals);

$redirect_to = $plang_message_url.'?call_back='.urlencode($call_back).'&reback_url='.urlencode($reback_url).'&act=pay&flag='.OAuthModelConfig::DEFAULT_CONST_FAILURE.'&lang='.$lang.'&errids='.json_encode(array(1025,1026,1027));
OAuthUtils::redirectTo($redirect_to);
exit;
?>
